<template>
  <div class="order_detail">
    <div class="order-goods">
      <van-card
        v-for="item in new_orderGoods"
        @click="
          item.giftId != undefined
            ? itemClick(item.giftId)
            : itemClick(item.goodsId)
        "
        :key="item.giftId != undefined ? item.giftId : item.id"
        :title="item.giftId != undefined ? item.giftName : item.goodsName"
        :desc="item.giftId != undefined ? '赠品' : item.specifications[0]"
        :num="item.giftId != undefined ? item.num : item.number"
        :price="item.giftId != undefined ? null : item.price"
        :thumb="item.giftId != undefined ? item.giftImg : item.picUrl"
      >
      </van-card>

      <van-cell-group>
        <van-cell title="商品金额">
          <span class="red">{{ (orderInfo.goodsPrice * 100) | yuan }}</span>
        </van-cell>
        <van-cell title="快递费用">
          <span class="red">到付</span>
        </van-cell>
      </van-cell-group>
    </div>

    <van-cell-group style="margin-top: 20px">
      <van-cell
        icon="dingwei"
        :title="`${orderInfo.consignee}  ${orderInfo.mobile}`"
        :label="orderInfo.address"
      />
    </van-cell-group>

    <van-cell-group style="margin-top: 20px">
      <van-cell title="下单时间">
        <span>{{ orderInfo.addTime }}</span>
      </van-cell>
      <van-cell title="订单编号">
        <span>{{ orderInfo.orderSn }}</span>
      </van-cell>
      <van-cell title="订单备注">
        <span>{{ orderInfo.remark }}</span>
      </van-cell>

      <van-cell title="应付款：">
        <span class="red">{{ (orderInfo.actualPrice * 100) | yuan }}</span>
      </van-cell>
      <van-cell title="订单状态">
        <span>{{ orderInfo.orderStatusText }}</span>
      </van-cell>
      <!-- 订单动作 -->
      <van-cell>
        <van-button
          size="small"
          v-if="handleOption.cancel"
          @click="cancelOrder(orderInfo.id)"
          style="margin-left: 10px"
          >取消订单</van-button
        >
        <van-button
          size="small"
          v-if="handleOption.pay"
          @click="toPay(orderInfo.id, orderInfo.payWay, orderInfo.payType)"
          style="margin-left: 10px"
          type="danger"
          >去支付</van-button
        >
        <van-button
          size="small"
          v-if="handleOption.delete"
          @click="deleteOrder(orderInfo.id)"
          style="margin-left: 10px"
          >删除订单</van-button
        >
        <van-button
          size="small"
          v-if="handleOption.confirm"
          @click="confirmOrder(orderInfo.id)"
          style="margin-left: 10px"
          type="danger"
          >确认收货</van-button
        >
        <van-button
          size="small"
          v-if="handleOption.refund"
          @click="refundOrder(orderInfo.id)"
          style="margin-left: 10px"
          type="default"
          >退款</van-button
        >
      </van-cell>
    </van-cell-group>

    <van-cell-group v-if="showExp()" style="margin-top: 20px">
      <van-cell title="快递公司">
        <span>{{ orderInfo.expCode }}</span>
      </van-cell>
      <van-cell title="快递编号">
        <span>{{ orderInfo.expNo }}</span>
      </van-cell>
    </van-cell-group>
    <van-steps
      direction="vertical"
      :active="0"
      active-color="#EE3F00"
      v-if="orderInfo.expCode != '自提'"
    >
      <van-step v-if="false"
        ><!--before v-if="traces.length == 0 && showExp()" -->
        <h3>已发货，正在等待快递员揽收</h3>
        <p>{{ orderInfo.shipTime }}</p>
      </van-step>
      <van-step v-else v-for="(trace, index) in traces" :key="index">
        <h3>{{ trace.acceptStation }}</h3>
        <p>{{ trace.acceptTime }}</p>
      </van-step>
    </van-steps>
  </div>
</template>

<script>
import {
  Card,
  Field,
  SubmitBar,
  Button,
  Cell,
  CellGroup,
  Dialog,
  Step,
  Steps,
} from 'vant';
import { getLocalStorage, setLocalStorage } from '@/utils/local-storage';
import _ from 'lodash';
import {
  orderDetail,
  orderDelete,
  orderConfirm,
  orderCancel,
  orderRefund,
  expressInfo,
  remindDelivery,
} from '@/api/api';

export default {
  data() {
    return {
      isSubmit: false,
      isDisabled: false,
      orderInfo: {},
      orderGoods: [],
      handleOption: {},
      expressInfo: {},
      traces: [],
      new_orderGoods: [],
    };
  },
  created() {
    this.init();
  },

  methods: {
    itemClick(id) {
      this.$router.push(`/items/detail/${id}`);
    },
    showExp() {
      return _.has(this.orderInfo, 'expNo');
    },
    deleteOrder(id) {
      let that = this;
      this.$dialog
        .confirm({ message: '确定要删除该订单吗?' })
        .then(() => {
          orderDelete({ orderId: id }).then(() => {
            this.$toast('已删除订单');
            this.$router.go(-1);
          });
        })
        .catch(() => {});
    },
    cancelOrder(id) {
      this.$dialog
        .confirm({ message: '确定要取消该订单吗?' })
        .then(() => {
          orderCancel({ orderId: id }).then(() => {
            this.init();
            this.$toast('已取消该订单');
          });
        })
        .catch(() => {});
    },
    confirmOrder(id) {
      this.$dialog
        .confirm({
          message: '请确认收到货物, 确认收货后无法撤销!',
        })
        .then(() => {
          orderConfirm({ orderId: id }).then(() => {
            this.init();
            this.$toast('已确认收货');
          });
        })
        .catch(() => {});
    },
    refundOrder(id) {
      this.$dialog
        .confirm({ message: '确定要申请退款吗?' })
        .then(() => {
          orderRefund({ orderId: id }).then(() => {
            this.init();
            this.$toast('已申请订单退款');
          });
        })
        .catch(() => {});
    },
    remindTheDelivery(id, orderNo, orderStatus, actualPrice) {
      let infoData = getLocalStorage('remindData');
      infoData.remindData = JSON.parse(infoData.remindData);
      if (
        infoData.remindData != null &&
        infoData.remindData.remindTime != this.getToday()
      ) {
        removeLocalStorage('remindData');
        infoData = getLocalStorage('remindData');
      }

      if (
        infoData.remindData != null &&
        infoData.remindData.remindTime == this.getToday() &&
        infoData.remindData.ids.indexOf(',' + id + ',') != -1
      ) {
        this.$dialog.alert({ message: '今日已经提醒发货，请勿重复提醒！' });
        return;
      }
      let sotrageIds = ',';
      if (infoData.remindData != null) {
        sotrageIds = infoData.remindData.ids;
      }

      this.$dialog.confirm({ message: '确定要提醒发货吗?' }).then(() => {
        remindDelivery({
          orderId: id,
          orderNo: orderNo,
          orderStatus: orderStatus,
          orderMoney: actualPrice,
        })
          .then((res) => {
            this.$toast('已提醒发货！');
            let remindObject = {};
            this.$set(remindObject, 'remindTime', this.getToday());
            this.$set(remindObject, 'ids', sotrageIds + id + ',');
            setLocalStorage({ remindData: JSON.stringify(remindObject) });
          })
          .catch((err) => {
            this.$toast('找不到对应客户经理，请联系管理员！');
          });
      });
    },
    getToday() {
      var now = new Date();
      var year = now.getFullYear(); //年
      var month = now.getMonth() + 1; //月
      var day = now.getDate(); //日
      var clock = year + '-';
      if (month < 10) clock += '0';
      clock += month + '-';
      if (day < 10) clock += '0';
      clock += day + ' ';
      return clock;
    },
    commentOrder(id) {},
    toPay(id, payWay, payType) {
      if (payWay == 1) {
        Dialog.alert({
          message:
            '您已选择线下支付，您的客户经理将会协助您完成线下支付，感谢您对喷喷涂涂的信任。',
        }).then(() => {});
        return;
      }
      this.$router.push({
        name: 'payment',
        query: { orderId: id, payType: payType },
      });
    },
    init() {
      let orderId = this.$route.query.orderId;
      let arr = new Array();
      arr.push(orderId);
      orderDetail({ orderIds: arr }).then((res) => {
        var data = res.data.data;
        this.orderInfo = data.orderInfos[0];
        this.orderGoods = data.orderGoods;

        this.new_orderGoods = [];
        for (var i = 0; i < this.orderGoods.length; i++) {
          this.new_orderGoods.push(this.orderGoods[i]);
          if (
            this.orderGoods[i].orderGiftList !== undefined &&
            this.orderGoods[i].orderGiftList.length > 0
          ) {
            for (var j = 0; j < this.orderGoods[i].orderGiftList.length; j++) {
              this.new_orderGoods.push(this.orderGoods[i].orderGiftList[j]);
            }
          }
        }
        this.handleOption = data.orderInfos[0].handleOption;
        this.expressInfo = data.expressInfo;
        if (
          this.expressInfo != undefined &&
          data.expressInfo.traces != undefined
        ) {
          this.traces = data.expressInfo.traces;
        }
      });
    },
  },

  components: {
    [Dialog.name]: Dialog,
    [CellGroup.name]: CellGroup,
    [Cell.name]: Cell,
    [Button.name]: Button,
    [SubmitBar.name]: SubmitBar,
    [Card.name]: Card,
    [Field.name]: Field,
    [Step.name]: Step,
    [Steps.name]: Steps,
  },
};
</script>


<style lang="scss" scoped>
.order_detail {
  padding-bottom: 70px;
}

/deep/ .van-cell__value--alone {
  display: flex;
  justify-content: flex-end;
}
</style>
